package Servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DBCon;

/**
 * Servlet implementation class EBIChangeIn
 */
@WebServlet("/EBIChangeIn")
public class EBIChangeIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EBIChangeIn() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = new String(request.getParameter("name").getBytes("iso-8859-1"), "utf-8");
		String workID = request.getParameter("workID");
		String Dname = new String(request.getParameter("D").getBytes("iso-8859-1"), "utf-8");
		System.out.println(Dname);
		String Pname = new String(request.getParameter("P").getBytes("iso-8859-1"), "utf-8");
		System.out.println(Pname);
		String Pstatus = new String(request.getParameter("PS").getBytes("iso-8859-1"), "utf-8");
		String sex = new String(request.getParameter("sex").getBytes("iso-8859-1"), "utf-8");
		String nation = new String(request.getParameter("nation").getBytes("iso-8859-1"), "utf-8");
		String education = new String(request.getParameter("education").getBytes("iso-8859-1"), "utf-8");
		String Mstatus = new String(request.getParameter("Mstatus").getBytes("iso-8859-1"), "utf-8");
		String address = new String(request.getParameter("address").getBytes("iso-8859-1"), "utf-8");
		String phone = request.getParameter("phone");
		String identify = request.getParameter("identify");
		
		try {
			DBCon dbCon = new DBCon();
			Connection conn = dbCon.getConnection();
			
			String sql = "Insert INTO ebit91 (Dname, Pname, Pstatus, workID, name, sex, nation, education, Mstatus, address, phone, identify) "
					+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";
			
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1,Dname);
			stmt.setString(2,Pname);
			stmt.setString(3,Pstatus);
			stmt.setString(4,workID);
			stmt.setString(5,name);
			stmt.setString(6,sex);
			stmt.setString(7,nation);
			stmt.setString(8,education);
			stmt.setString(9,Mstatus);
			stmt.setString(10,address);
			stmt.setString(11,phone);
			stmt.setString(12,identify);
			
			stmt.executeUpdate();
			
			stmt.close();
			conn.close();
			
			response.sendRedirect("EBISearch.jsp");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
